#!/bin/bash

mkdir -p /var/log

load_module()
{
	module_name=$1
	if modinfo $module_name >/dev/null 2>&1; then
		echo "loading..." $module_name
		modprobe $module_name
	else
		echo "$module_name no exists"
	fi
}


create_group() {
	egrep "^$1" /etc/group >& /dev/null
	if [ $? -ne 0 ]; then
		groupadd $1
	fi
}

function in_ramfs()
{
	/usr/sbin/netlink-user

	load_module virtio_net
	load_module ngbe

	HOSTNAME=uos
	echo "${HOSTNAME}" | tee /etc/hostname

	cat > /etc/hosts << EOF
127.0.0.1       localhost
127.0.0.1       ${HOSTNAME}

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF

	cat >> /root/.bash_profile << EOF
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/profile
fi
export TERM=linux
export HOME=/root
export LANG=zh_CN.UTF-8
export LC_CTYPE="zh_CN.UTF-8"
EOF
	cp /root/.bash_profile /root/.bashrc

	echo "export TERM=linux" >> /etc/bashrc

	echo "#!/bin/bash" > /bin/hostname
	echo "echo 'uos.localdomain'" >> /bin/hostname
	chmod +x /bin/hostname

	#echo "root:1" | /usr/sbin/chpasswd
	#echo "1" | passwd --stdin root 

	ip link set dev eth0 up
	ip addr add 10.2.21.180/24 dev eth0
	ip route add default via 10.2.21.1

	create_group sshd
	[ -d /var/lib/sshd ] ||  mkdir -p /var/lib/sshd
	/usr/sbin/useradd -u 66 -g sshd -c "SSH daemon" -d /var/lib/sshd -s /sbin/nologin -m sshd

	[ -f /etc/ssh/ssh_host_rsa_key ] || /usr/bin/ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key
	#[ -f /etc/ssh/ssh_host_ecdsa_key ] || /usr/bin/ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_ecdsa_key
	#[ -f /etc/ssh/ssh_host_ed25519_key ] ||  /usr/bin/ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_ed25519_key
	[ -d /var/run/sshd ] || mkdir -p /var/run/sshd
	#/usr/libexec/openssh/sshd-keygen& >/dev/null 2>&1 &
	/usr/sbin/sshd -D >/dev/null 2>&1 &

	touch /root/.vimrc


	setsid -c /bin/bash

	/bin/killall sshd
	ip addr del 10.2.21.180/24 dev eth0
	#dhclient eth0
	#ip link set dev eth0 down
	#ip link set dev eth0 up
	rmmod virtio_net
	rmmod ngbe
}
